package leetcode.easy;

/**
 * ClassName: $88_MergeTwoSortedArray
 * Description:
 * date: 2021-10-21 11:07
 *
 * @author liyifan
 */
public class $88_MergeTwoSortedArray {
    public int[] merge(int[] nums1, int m, int[] nums2, int n) {
        int[] res = new int[m + n];
        int i = 0,j = 0;
        int k = 0;
        while (i < m && j < n) {
            if (nums1[i] < nums2[j]) {
                // 插入nums1[i]
                res[k++] = nums1[i++];
            } else if (nums1[i] > nums2[j]) {
                // 插入nums2[j]
                res[k++] = nums2[j++];
            } else {
                res[k++] = nums1[i++];
            }
        }
        if (i == m) {
            // nums1全部插入
            while (j < n) {
                res[k++] = nums2[j++];
            }
        }

        if (j == n) {
            // nums全部插入
            while (i < m) {
                res[k++] = nums1[i++];
            }
        }
        return res;
    }
}
